package com.sgzn.sgpay.service.tcp.receive.handler;

import com.sgzn.sgpay.enums.AppName;
import com.sgzn.sgpay.model.request.SgsCreditIncreaseCallBackRequest;
import com.sgzn.sgpay.service.converter.AssembleSgsCommonHandle;
import com.sgzn.sgpay.service.tcp.SgTcpServer;
import com.sgzn.sgpay.service.tcp.receive.TcpReceiveCallHandlerTemplate;
import com.sgzn.sgpay.service.tcp.receive.modle.TcpReceiveCallRequest;
import com.sgzn.sgpay.util.SerialNoUtil;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 云上分应答 处理器
 * @author huangsenguang
 * @version Id: TcpIncreaseCreditReceiveHandler.java, v 0.1 2018/7/27 22:42 huangsenguang Exp $$
 */
@Service
@Slf4j
public class TcpIncreaseCreditReceiveHandler extends TcpReceiveCallHandlerTemplate<Boolean> {
    /**
     * 协议总字符数
     */
    @Getter
    private final int protocolLength = 14;

    @Override
    public Boolean callInner(TcpReceiveCallRequest request) {
        return isSuccess(request);
    }

    @Override
    protected void handleAfter(TcpReceiveCallRequest request, Boolean result) {
        //去订单里拿金额
        //更新用户余额-
        //更新商户余额+
        //更新订单状态AFT-->SUC
        SgsCreditIncreaseCallBackRequest cbRequest = new SgsCreditIncreaseCallBackRequest();
        cbRequest.setInitiationID(SerialNoUtil.generateSerialNo(AppName.SGT));
        cbRequest.setLoginChannel(AppName.SGT.getCode());
        cbRequest.setReqTime(new Date());
        cbRequest.setDeviceId(request.getDeviceId());
        log.info("调用上分回调接口 ");
        try {
            SgTcpServer.sgsCreditIncreaseCallBackService.invoke(cbRequest);
        }catch (Exception e){
            log.error("调用上分回调接口失败",e);
        }



    }

    @Override
    protected int getProtocolLength(TcpReceiveCallRequest request) {
        return this.protocolLength;
    }
}

